package com.bicon.botu.mall.order.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.mall.order.dto.ChildAndDetailDto;
import com.bicon.botu.mall.order.dto.OrderChildDto;
import com.bicon.botu.mall.order.dto.OrderTotalDto;
import com.bicon.botu.mall.order.service.IOrderChildService;
import com.bicon.botu.mall.order.service.IOrderGoodsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 订单前端控制器
 * </p>
 *
 * @author 李双喜
 * @since 2018-05-14
 */
@RestController
@RequestMapping("/V3.0.0/order-child")
public class OrderChildControllerV3 extends BaseController {
    @Autowired
    private IOrderChildService iOrderChildService;

    @Autowired
    private IOrderGoodsService iOrderGoodsService;

    /**
     * 订单详情接口
     *
     * @param orderGoodsId 分组订单id
     * @return
     */
    @RequestMapping("/order-detail")
    public BaseResult selectOrderDetail(@RequestParam String orderGoodsId) {
        if (StringUtils.isEmpty(orderGoodsId)) {
            return new BaseResult(400, "参数错误");
        }
        return iOrderChildService.selectOrderDetail(orderGoodsId);

    }

    /**
     * 订单统计接口
     *
     * @param storeId 店铺id
     * @return
     */
    @RequestMapping("/order-total")
    public BaseResult selectOrderTotal(@RequestParam String storeId) {
        if (StringUtils.isEmpty(storeId)) {
            return new BaseResult(400, "参数错误");
        }
        OrderTotalDto orderTotalDto = iOrderChildService.selectOrderTotal(storeId);
        return new BaseResult(200, "success", orderTotalDto);
    }

    /**
     * 根据订单ID 查询子订单 商品信息列表
     * @param orderGoodsId
     * @return
     */
    @RequestMapping(value = "getOrderInfoByOrderGoodsId",method = RequestMethod.POST)
    public BaseResult getOrderInfoByOrderGoodsId(String orderGoodsId,
                                                 @RequestParam(defaultValue = "0") Integer pageNo,
                                                 @RequestParam(defaultValue = "10") Integer pageSize){
        //数据校验
        if(StringUtils.isNotBlank(orderGoodsId)){
            //封装返回数据
            JSONObject resultJson=new JSONObject();
            //获得总订单对象，从对象获得商品总价
            double sumTotalPrice=iOrderChildService.getSumTotalPrice(orderGoodsId);
            //获得子订单 对象列表
            Page<ChildAndDetailDto> childAndDetailDtoPage=iOrderChildService
                    .getOrderInfoByOrderGoodsId(new Page<ChildAndDetailDto>(pageNo,pageSize),orderGoodsId);
            //组装返回娄
            resultJson.put("totalPrice",sumTotalPrice);
            resultJson.put("productList",childAndDetailDtoPage);
            return sendResult200(resultJson);
        }else{
            return sendResult400();
        }
    }

    /**
     * 结算并修改订单的运费
     *
     * @param orderGoodsId 订单Id
     * @param province     省
     * @return
     */
    @RequestMapping("/update-freight")
    public BaseResult updateFreight(@RequestParam String orderGoodsId, @RequestParam String province) {
        if (StringUtils.isEmpty(orderGoodsId) || StringUtils.isEmpty(province) ) {
            return new BaseResult(400, "参数错误");
        }
        List<String> goodsId = new ArrayList<>();
        goodsId.add(orderGoodsId);
        OrderChildDto result = iOrderChildService.updatefreight(goodsId, province);
        return sendResult200(result);
    }
}
